package pl.edu.pk.cc.client.connections;

import java.util.List;
import java.util.Vector;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import pl.edu.pk.cc.client.CCMain;
import pl.edu.pk.cc.common.dto.CCAgreementDTO;
import pl.edu.pk.cc.common.dto.CCCallHistoryDTO;
import pl.edu.pk.cc.common.dto.CCClientDTO;
import pl.edu.pk.cc.common.dto.CCEmploymentDTO;
import pl.edu.pk.cc.common.dto.CCProjectsDTO;
import pl.edu.pk.cc.common.dto.CCResourceDTO;
import pl.edu.pk.cc.common.dto.CCSaleDTO;
import pl.edu.pk.cc.common.dto.CCWorkerDTO;
import pl.edu.pk.cc.common.util.CCSession;

public class CCServerCallsExecutor {

	private static ExecutorService threadExecutor = Executors.newSingleThreadExecutor();

	private CCServerCallsExecutor() {
	}

	protected static boolean login(final String login, final String password, final String url) {
		try {
			return threadExecutor.submit(new Callable<Boolean>() {
				@Override
				public Boolean call() throws Exception {
					if (!CCServletCalls.getConnectionURL().equals(url)) {
						CCServletCalls.init(url);
					}
					Vector<Object> res = CCServletCalls.login(login, password);
					if (res == null) {
						return false;
					} else {
						CCMain.setCurrentSession((CCSession) res.get(0));
						CCMain.setCurrentWorker((CCWorkerDTO) res.get(1));
					}
					return true;
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	protected static void logout() {
		threadExecutor.submit(new Runnable() {
			@Override
			public void run() {
				CCServletCalls.logout(CCMain.getCurrentSession().getSessionID());
			}
		});
	}

	/**
	 * protected static List<CCResourceDTO> getResourceByID(final String table, final Vector<Object> args) { Future<List<CCResourceDTO>> result = threadExecutor.submit(new
	 * Callable<List<CCResourceDTO>>() {
	 * 
	 * @Override public List<CCResourceDTO> call() throws Exception { args.add(0, table); return CCServletCalls.INSTANCE.getResourceByID (CCMain.getCurrentSession().getSessionID(), args); } });
	 * 
	 *           while (result.isDone()) { }
	 * 
	 *           try { return result.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return null; }
	 * 
	 *           protected static List<CCResourceDTO> getResourceByString(final String table,final String column, final Vector<Object> args) { Future<List<CCResourceDTO>> result =
	 *           threadExecutor.submit(new Callable<List<CCResourceDTO>>() {
	 * @Override public List<CCResourceDTO> call() throws Exception { args.add(0, column); return CCServletCalls.INSTANCE.getResourceByString (CCMain.getCurrentSession().getSessionID(), args); } });
	 * 
	 *           while (result.isDone()) { }
	 * 
	 *           try { return result.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return null; }
	 */

	protected static Vector<CCAgreementDTO> getAgreementByID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCAgreementDTO>>() {
				@Override
				public Vector<CCAgreementDTO> call() throws Exception {
					return CCServletCalls.getAgreementByID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCAgreementDTO> getAgreementByClientID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCAgreementDTO>>() {
				@Override
				public Vector<CCAgreementDTO> call() throws Exception {
					return CCServletCalls.getAgreementByClientID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCCallHistoryDTO> getCallHistoryByID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCCallHistoryDTO>>() {
				@Override
				public Vector<CCCallHistoryDTO> call() throws Exception {
					return CCServletCalls.getCallHistoryByID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCCallHistoryDTO> getCallHistoryByClientID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCCallHistoryDTO>>() {
				@Override
				public Vector<CCCallHistoryDTO> call() throws Exception {
					return CCServletCalls.getCallHistoryByClientID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	protected static Vector<CCCallHistoryDTO> getCallHistoryByWorkerID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCCallHistoryDTO>>() {
				@Override
				public Vector<CCCallHistoryDTO> call() throws Exception {
					return CCServletCalls.getCallHistoryByWorkerID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCClientDTO> getClientByID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCClientDTO>>() {
				@Override
				public Vector<CCClientDTO> call() throws Exception {
					return CCServletCalls.getClientByID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCClientDTO> getClientByName(final Vector<String> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCClientDTO>>() {
				@Override
				public Vector<CCClientDTO> call() throws Exception {
					return CCServletCalls.getClientByName(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCClientDTO> getClientByAddress(final Vector<String> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCClientDTO>>() {
				@Override
				public Vector<CCClientDTO> call() throws Exception {
					return CCServletCalls.getClientByAddress(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCEmploymentDTO> getEmploymentByID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCEmploymentDTO>>() {
				@Override
				public Vector<CCEmploymentDTO> call() throws Exception {
					return CCServletCalls.getEmploymentByID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCEmploymentDTO> getEmploymentByPlacement(final Vector<String> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCEmploymentDTO>>() {
				@Override
				public Vector<CCEmploymentDTO> call() throws Exception {
					return CCServletCalls.getEmploymentByPlacement(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCEmploymentDTO> getEmploymentByWorkerID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCEmploymentDTO>>() {
				@Override
				public Vector<CCEmploymentDTO> call() throws Exception {
					return CCServletCalls.getEmploymentByWorkerID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCSaleDTO> getSaleByID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCSaleDTO>>() {
				@Override
				public Vector<CCSaleDTO> call() throws Exception {
					return CCServletCalls.getSaleByID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCSaleDTO> getSaleByClientID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCSaleDTO>>() {
				@Override
				public Vector<CCSaleDTO> call() throws Exception {
					return CCServletCalls.getSaleByClientID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCWorkerDTO> getWorkerByID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCWorkerDTO>>() {
				@Override
				public Vector<CCWorkerDTO> call() throws Exception {
					return CCServletCalls.getWorkerByID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCWorkerDTO> getWorkerByName(final Vector<String> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCWorkerDTO>>() {
				@Override
				public Vector<CCWorkerDTO> call() throws Exception {
					return CCServletCalls.getWorkerByName(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static Vector<CCWorkerDTO> getWorkerByLogin(final Vector<String> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCWorkerDTO>>() {
				@Override
				public Vector<CCWorkerDTO> call() throws Exception {
					return CCServletCalls.getWorkerByLogin(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected static void saveData(final Vector<CCResourceDTO> params) {
		try {
			threadExecutor.submit(new Runnable() {
				@Override
				public void run() {
					CCServletCalls.saveData(params);
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Vector<CCClientDTO> getClientByWorkerID(final Vector<Long> params) {
		try {
			return threadExecutor.submit(new Callable<Vector<CCClientDTO>>() {
				@Override
				public Vector<CCClientDTO> call() throws Exception {
					return CCServletCalls.getClientByWorkerID(params);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static List<CCAgreementDTO> getAgreementAll() {
		try {
			return threadExecutor.submit(new Callable<Vector<CCAgreementDTO>>() {
				@Override
				public Vector<CCAgreementDTO> call() throws Exception {
					return CCServletCalls.getAgreementAll();
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<CCProjectsDTO> getProjectsAll() {
		try {
			return threadExecutor.submit(new Callable<Vector<CCProjectsDTO>>() {
				@Override
				public Vector<CCProjectsDTO> call() throws Exception {
					return CCServletCalls.getProjectsAll();
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static List<CCClientDTO> getClientsRand() {
		try {
			return threadExecutor.submit(new Callable<Vector<CCClientDTO>>() {
				@Override
				public Vector<CCClientDTO> call() throws Exception {
					return CCServletCalls.getClientsRand();
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Vector<String> getScriptByType(final String type) {
		try {
			return threadExecutor.submit(new Callable<Vector<String>>() {
				@Override
				public Vector<String> call() throws Exception {
					return CCServletCalls.getScriptByType(type);
				}
			}).get();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
